package top.starmc.starpet.util;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class SlotUtil {
    // 这个类直接用就行,不用在乎咋写的

    public static ItemHandler handleSlotItem(ItemStack slotItem, ItemStack handItem, int mouseButton) {
        return new ItemHandler(slotItem, handItem, mouseButton);
    }

    public static class ItemHandler {
        private ItemStack slotAfter;
        private ItemStack handAfter;
        private boolean modify;

        private ItemHandler(ItemStack slotItem, ItemStack handItem, int mouseButton) {
            this.handle(slotItem, handItem, mouseButton);
        }

        public static ItemStack setAmount(ItemStack itemStack, int amount) {
            itemStack.setAmount(amount);
            return itemStack;
        }

        public void handle(ItemStack slotItem, ItemStack handItem, int mouseButton) {
            slotItem = slotItem == null ? new ItemStack(Material.AIR) : slotItem.clone();
            handItem = handItem == null ? new ItemStack(Material.AIR) : handItem.clone();

            //左键点击槽位
            if (mouseButton == 0) {
                //相同物品则把手上物品放入
                // 如果槽位里有东西,并且手上有东西
                if (slotItem.getType() != Material.AIR && handItem.getType() != Material.AIR) {
                    if (slotItem.isSimilar(handItem)) {
                        int amount = slotItem.getMaxStackSize() - slotItem.getAmount();//最大可放入的数量
                        int addAmount = Math.min(amount, handItem.getAmount());//将要放入的数量
                        if (addAmount > 0) {
                            setItemStack(setAmount(slotItem, slotItem.getAmount() + addAmount));
                            setCursorItem(setAmount(handItem, handItem.getAmount() - addAmount));
                        }
                    } else {
                        setItemStack(handItem);
                        setCursorItem(slotItem);
                    }
                } else if (handItem.getType() != Material.AIR || slotItem.getType() != Material.AIR) {
                    //否则交换物品
                    setItemStack(handItem);
                    setCursorItem(slotItem);
                }


            }
            //右键点击槽位
            if (mouseButton == 1) {
                //如果格子是空的，且手上有物品  ->  放置一个进去
                if (slotItem.getType() == Material.AIR && handItem.getType() != Material.AIR) {
                    setItemStack(setAmount(handItem.clone(), 1));
                    setCursorItem(setAmount(handItem, handItem.getAmount() - 1));
                }
                //如果格子不是空的，手上是空的  ->  取出格子的一半物品到手上
                if (slotItem.getType() != Material.AIR && handItem.getType() == Material.AIR) {
                    if (slotItem.getAmount() == 1) {
                        //如果格子只有一个物品，直接交换物品即可
                        setItemStack(handItem);
                        setCursorItem(slotItem);
                    } else {
                        //否则取出一半物品到手上
                        int amount = slotItem.getAmount() / 2; //amount 为取出的数量
                        setItemStack(setAmount(slotItem, slotItem.getAmount() - amount));
                        setCursorItem(setAmount(slotItem.clone(), amount));
                    }
                }
                //如果格子不是空的，手上也不是空的  ->  判断格子与手上物品是否相同，且数量还未满  若满足则放置一个物品到格子内
                if (slotItem.getType() != Material.AIR && handItem.getType() != Material.AIR) {
                    if (slotItem.getAmount() < slotItem.getMaxStackSize()) {
                        if (slotItem.isSimilar(handItem)) {
                            setItemStack(setAmount(slotItem, slotItem.getAmount() + 1));
                            setCursorItem(setAmount(handItem, handItem.getAmount() - 1));
                        } else {
                            setItemStack(handItem);
                            setCursorItem(slotItem);
                        }
                    }
                }
            }
        }

        public void setItemStack(ItemStack itemStack) {
            slotAfter = itemStack;
            modify = true;
        }

        public void setCursorItem(ItemStack itemStack) {
            if (itemStack == null || itemStack.getAmount() <= 0) {
                handAfter = new ItemStack(Material.AIR);
            } else {
                handAfter = itemStack;
            }
            modify = true;
        }

        public ItemStack getSlotAfter() {
            return slotAfter;
        }

        public ItemStack getHandAfter() {
            return handAfter;
        }

        public boolean isModify() {
            return modify;
        }
    }
}
